namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorExponentialMovingAverageCross : BaseIndicator
    {
        private CandleShape cs;
        private MarkShape smsBs;
        private string target = ("EMA.Target." + Common.GetGuid());
        private LineShape tlsBuy;
        private LineShape tlsSell;
        private int type;

        public IndicatorExponentialMovingAverageCross()
        {
            base.IndType = IndicatorType.BS;
            base.IndName = "BS";
            base.IndDesc = " 指数移动平均交叉线";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 6.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 5.0));
            base.fields = new string[] { "BUY", "SELL", "SMA" };
        }

        public override bool Contains(object obj)
        {
            if (((obj != this.tlsBuy) && (obj != this.tlsSell)) && (obj != this.smsBs))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = 0.0;
            double num2 = 0.0;
            double num3 = 0.0;
            if (this.type == 0)
            {
                double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[r][this.cs.CloseField]);
                double num5 = Convert.ToDouble(base.Crab.DataSource.Rows[r][this.cs.HighField]);
                double num6 = Convert.ToDouble(base.Crab.DataSource.Rows[r][this.cs.LowField]);
                num3 = ((num4 + num5) + num6) / 3.0;
                curValue = Common.CalculateExponentialMovingAvg(this.TlsBuy.Name, this.target, (int) base.IndPeriod[0].ParamValue, num3, base.Crab.DataSource, r);
                num2 = Common.CalculateExponentialMovingAvg(this.tlsSell.Name, this.tlsBuy.Name, (int) base.IndPeriod[1].ParamValue, curValue, base.Crab.DataSource, r);
            }
            return new double[] { curValue, num2, num3 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsBuy, this.tlsSell, this.smsBs };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsBuy);
            base.Crab.DeleteShape(this.TlsSell);
            base.Crab.DeleteShape(this.SmsBs);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                if ((((target == this.Cs.CloseField) || (target == this.Cs.HighField)) || ((target == this.Cs.LowField) || (target == null))) && ((!(row[this.Cs.CloseField] is DBNull) && !(row[this.Cs.HighField] is DBNull)) && !(row[this.Cs.LowField] is DBNull)))
                {
                    double[] numArray = this.Get(r);
                    base.Crab.SetValue(this.TlsBuy.Name, numArray[0], r);
                    base.Crab.SetValue(this.TlsSell.Name, numArray[1], r);
                    base.Crab.SetValue(this.Target, numArray[2], r);
                    int index = r - 1;
                    if (index < 0)
                    {
                        index = 0;
                    }
                    double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[index][this.TlsBuy.Name]);
                    double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[index][this.TlsSell.Name]);
                    Common.GetDateTimeByTimeKey(base.Crab.DataSource.Rows[index][base.Crab.CalenderScaleField].ToString());
                    if (numArray[0] >= numArray[1])
                    {
                        base.Crab.SetCandleColor(this.Cs.Name, Color.Red, r);
                        base.Crab.SetCandleColor(this.Cs.Name, Color.Red, index);
                        if (num2 < num3)
                        {
                            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[index][this.Cs.LowField]);
                            base.Crab.SetMarkValue(this.SmsBs.Name, num4, MarkStyle.UpArrow, "买入", index);
                        }
                    }
                    else
                    {
                        base.Crab.SetCandleColor(this.Cs.Name, Color.Lime, r);
                        base.Crab.SetCandleColor(this.Cs.Name, Color.Lime, index);
                        if (num2 >= num3)
                        {
                            double num5 = Convert.ToDouble(base.Crab.DataSource.Rows[index][this.Cs.HighField]);
                            base.Crab.SetMarkValue(this.SmsBs.Name, num5, MarkStyle.DownArrow, "卖出", index);
                        }
                    }
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            this.TlsBuy = base.Crab.AddLine(fields[0], panelID);
            this.SmsBs = base.Crab.AddMark(panelID, Common.GetGuid(), fields[2], 0.01, 2);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsSell = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[1], fields[1], Color.Yellow, 1, 0.01, 2, DashStyle.Solid);
            base.Crab.DataSource.Columns.Add(this.Target);
            foreach (ChartDiv div in base.Crab.DicChartDiv.Values)
            {
                foreach (BaseShape shape in div.ShapeDic.Values)
                {
                    if (shape.Name == source[0])
                    {
                        this.cs = shape as CandleShape;
                    }
                }
            }
        }

        public CandleShape Cs
        {
            get
            {
                return this.cs;
            }
            set
            {
                this.cs = value;
            }
        }

        public MarkShape SmsBs
        {
            get
            {
                return this.smsBs;
            }
            set
            {
                this.smsBs = value;
            }
        }

        public string Target
        {
            get
            {
                return this.target;
            }
            set
            {
                this.target = value;
            }
        }

        public LineShape TlsBuy
        {
            get
            {
                return this.tlsBuy;
            }
            set
            {
                this.tlsBuy = value;
            }
        }

        public LineShape TlsSell
        {
            get
            {
                return this.tlsSell;
            }
            set
            {
                this.tlsSell = value;
            }
        }

        public int Type
        {
            get
            {
                return this.type;
            }
            set
            {
                this.type = value;
            }
        }
    }
}

